package com.xpsoft.oa.service.produce.impl;


import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.google.gson.reflect.TypeToken;
import com.xpsoft.core.service.impl.BaseServiceImpl;
import com.xpsoft.core.util.JsonUtil;
import com.xpsoft.oa.dao.produce.MaterialDao;
import com.xpsoft.oa.dao.produce.MaterialDivertDao;
import com.xpsoft.oa.dao.produce.MaterialDivertInfoDao;
import com.xpsoft.oa.dao.produce.ProjectRepertoryDao;
import com.xpsoft.oa.dao.project.ProjectDao;
import com.xpsoft.oa.model.produce.Material;
import com.xpsoft.oa.model.produce.MaterialDivert;
import com.xpsoft.oa.model.produce.MaterialDivertInfo;
import com.xpsoft.oa.model.produce.ProjectRepertory;
import com.xpsoft.oa.model.project.Project;
import com.xpsoft.oa.service.produce.MaterialDivertService;

public class MaterialDivertServiceImpl extends BaseServiceImpl<MaterialDivert> implements MaterialDivertService{
	private MaterialDivertDao dao;
	@Autowired
	private MaterialDivertInfoDao materialDivertInfoDao;
	@Autowired
	private MaterialDao materialDao;
	@Autowired
	private ProjectRepertoryDao projectRepertoryDao;
	@Autowired
	private ProjectDao projectDao;
	public MaterialDivertServiceImpl(MaterialDivertDao dao) {
		super(dao);
		this.dao=dao;
	}

	@Override
	public void saveMaterialDivert(MaterialDivert materialDivert,
			String dataList,boolean isProjectRep) {
		// TODO Auto-generated method stub
		dao.save(materialDivert);
		Boolean isNew = true;//新增标志
		if(materialDivert.getId()!=null){
			isNew = false;
		}
		dao.save(materialDivert);
		if(StringUtils.isNotBlank(dataList)){
			List<MaterialDivertInfo> materialDivertInfoList = JsonUtil.fromJsonTypes(dataList,
					new TypeToken<List<MaterialDivertInfo>>() {
					}.getType());
			if(!isNew){//修改
				List<Long> idList = new ArrayList<Long>();
				for(MaterialDivertInfo materialDivertInfo:materialDivertInfoList){
					if(materialDivertInfo.getId()!=null){//修改的记录
						idList.add(materialDivertInfo.getId());
					}
				}
				if(idList.size()>0){//先进行删除
					materialDivertInfoDao.deleteMaterialDivertInfos(materialDivert.getId(), idList);
				}
			}
			for(MaterialDivertInfo materialDivertInfo:materialDivertInfoList){
				materialDivertInfo.setMaterialDivert(materialDivert);
				materialDivertInfoDao.save(materialDivertInfo);
				Material material = materialDao.get(materialDivertInfo.getMaterial().getId());
				if(isProjectRep){//项目仓库调出，总部仓库调入
					Long proId = materialDivert.getFromProject().getId();
					Project project = projectDao.get(proId);
					ProjectRepertory projectRepertory = projectRepertoryDao.getRepertory(proId, material.getId());
					if(projectRepertory!=null){
						projectRepertory.setNum(projectRepertory.getNum()-materialDivertInfo.getNum());
					}else{
						projectRepertory = new ProjectRepertory();
						projectRepertory.setMaterial(material);
						projectRepertory.setProject(project);
						projectRepertory.setNum(0);
					}
					projectRepertoryDao.save(projectRepertory);
					material.setNum(material.getNum()+materialDivertInfo.getNum());//总部仓库增加
					materialDao.save(material);
				}else{//总部仓库调出。项目仓库调入
					Long proId = materialDivert.getToProject().getId();
					Project project = projectDao.get(proId);
					ProjectRepertory projectRepertory = projectRepertoryDao.getRepertory(proId, material.getId());
					if(projectRepertory!=null){
						projectRepertory.setNum(projectRepertory.getNum()+materialDivertInfo.getNum());
					}else{
						projectRepertory = new ProjectRepertory();
						projectRepertory.setMaterial(material);
						projectRepertory.setProject(project);
						projectRepertory.setNum(materialDivertInfo.getNum());
					}
					projectRepertoryDao.save(projectRepertory);
					material.setNum(material.getNum()-materialDivertInfo.getNum());//总部仓库减少
					materialDao.save(material);
				}
			}
		}
	}

}